<?php
/**
 * 后台系统用户管理文件
 *
 * @package         YDCMS
 * @author          QQ:644157559 <644157559@qq.com>
 * @version         $Id: SysAdminUserController.class.php v1.0 2014-06-12 17:27:03 $
 */
namespace Admin\Controller;

class SysAdminUserController extends CommonController {
    protected $model = NULL;
    protected $service = NULL;
    
    public function _initialize() {
        parent::_initialize();
        $this->model = D("Admin");
        $this->service = D("Admin", "Service");
    }

    public function index() {
        $result = $this->getPagination("Admin");
        //var_dump($result);die;
        //$SysUserList = $this->model->getSysUserList();
        //$SysUserCount = $this->model->getCount();
        //var_dump($SysUserList);die;
        $this->assign('SysUserList', $result['data']);
        $this->assign('SysUserCount', $result['total_rows']);
        $this->assign('page', $result['show']);
        $this->display();
    }
    
    /**
     * 添加管理员页面
     */
    public function add() {
        $roleList = D('Roles', 'Service')->getRoles(array('status' => 1));
        $this->assign('roleList', $roleList);
        //var_dump($this->roleList);die;
        $this->display();
    }
    
    /**
     * 创建管理员
     * @return type
     */
    public function create() {
        $userinfo = I("post.admin");
        if(!isArrayAndNotEmpty($userinfo)){
            return $this->errorReturn('无效的操作！');
        }
        $result = $this->service->addSysUser($userinfo);
        if(!$result['status']){
            return $this->errorReturn($result['data']['error']);
        }
        return $this->successReturn('添加管理员成功！', U('SysAdminUser/index'));
    }
    
    /**
     * 修改管理员页面
     */
    public function edit() {
        $id = I('get.id', 0, 'intval');
        if($id <= 0){
            $this->error("参数有误");
        }
        $userinfo = $this->model->getById($id);
        $roleList = D('Roles', 'Service')->getRoles(array('status' => 1));
        $this->assign('roleList', $roleList);
        $this->assign('userinfo', $userinfo);
        //var_dump($this->roleList);die;
        $this->display();
    }
    /**
     * 更新管理员信息
     */
    public function update() {
        $userinfo = I("post.admin");
        if(!isArrayAndNotEmpty($userinfo)){
            return $this->errorReturn('无效的操作！');
        }
        $result = $this->service->updateSysUser($userinfo);
        if(!$result['status']){
            return $this->errorReturn($result['data']['error']);
        }
        return $this->successReturn('修改管理员成功！', U('SysAdminUser/index'));
    }
    
    /**
     * 删除管理员
     */
    public function delete() {
        $id = I("get.id", 0, "intval");
        if($id <= 0){
            return $this->errorReturn('无效的操作！');
        }
        $result = $this->service->deleteSysUser($id);
        if(!$result['status']){
            return $this->errorReturn($result['data']['error']);
        }
        return $this->successReturn('删除管理员成功！', U('SysAdminUser/index'));
    }
    
    public function toggleStatus() {
        $id = I("post.id", 0, "intval");
        $status = I("post.status", NULL);
        $SysUserInfo = $this->service->getSysUserInfo($id);
        if (!isset($id) || !isArrayAndNotEmpty($SysUserInfo)) {
            return $this->errorReturn('无效的操作！');
        }
        if($SysUserInfo['id'] == 1 || $SysUserInfo['is_super'] == 1){
            return $this->errorReturn('超级管理员不能禁用！');
        }
        if ($status == 'yes') {
            $this->service->setStatus($id, 1);
        } else if($status === 'no'){
            $this->service->setStatus($id, 0);
        }else{
            return $this->errorReturn('无效的操作！');
        }
        $info = $status === 'no' ? '禁用成功！' : '启用成功！';
        $this->successReturn($info);
    }
}
